package yxy.game.pm2.active.module.paylevelpackage;

import org.joda.time.DateTime;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.bean.actgraderecharge;
import yxy.game.pm2.active.module.ActOpenHandle;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.OpenType;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.lockorder.LockOrderInfo;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class ActiveModule_支付等级礼包 extends PlayerModule {

	//bean ---------------------------------------------------
	
	public static final int acttype = 21;
	
	private HashMap<Integer, PLPInfo> plps = new HashMap<Integer, PLPInfo>();
	private ArrayList<Integer> opentype = new ArrayList<Integer>();
	
	public HashMap<Integer, PLPInfo> getPlps() {
		return plps;
	}
	public void setPlps(HashMap<Integer, PLPInfo> plps) {
		this.plps = plps;
	}
	public ArrayList<Integer> getOpentype() {
		return opentype;
	}
	public void setOpentype(ArrayList<Integer> opentype) {
		this.opentype = opentype;
	}
	
	//fnc -----------------------------------------------------

//	public static final int canget = 1;
//	public static final int hadget = 2;
//	public static final int cannotget = 3;
	
	/**
	 * @param pcode
	 */
	public static ActiveModule_支付等级礼包 create(String pcode) {
		ActiveModule_支付等级礼包 am = new ActiveModule_支付等级礼包();
		am.setPlayerCode(pcode);
		am.set();
		return am;
	}

	public void update() {
		boolean save = false;
		
		long now = DateTime.now().getMillis();
		for(PLPInfo plp: getPlps().values()) {
			
			//检查是否结束
			long endtime = plp.getEndtime();
			if(now - endtime > 0) {
				plp.setOpen(false);
				save = true;
			}
			
			if(plp.getLock() == null) {
				continue;
			}
			
			//检查锁单
			long lock = plp.getLock().getMillis();
			if(now - lock > LockOrderInfo.lockTime()) {
				plp.setLock(null);
				save = true;
			}
		}
		
		//检查等级
		PlayerBean pb = PlayerBean.get(getPlayerCode());
		int level = pb.getLevel();
		Map<String, actgraderecharge> execlmap = ActDataCenter.getAllData(actgraderecharge.class, getPlayerCode());
		for(actgraderecharge execl : execlmap.values()) {
			if(level < execl.getCondition()) {
				continue;
			}
			
			if(getPlps().containsKey(execl.getId())) {
				continue;
			}
			
			PLPInfo newplp = new PLPInfo();
			newplp.setId(execl.getId());
			newplp.setGoodsid(execl.getChargeid());
			
			DateTime end = DateTime.now().plusSeconds(execl.getTime());
			newplp.setEndtime(end.getMillis());
			newplp.setOpen(true);
			
			getPlps().put(execl.getId(), newplp);
			set();
				
			{
				if(level >= execl.getCondition() && !getOpentype().contains(execl.getId())) {
					new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.礼包新开启);
					getOpentype().add(execl.getId());
					save = true;
				}
			}
		}
		
		if(save) {
			set();
		}
	}

	/**
	 * 能否购买礼包
	 * @param goodsid
	 * @return
	 */
	public int canBuyGiftPackage(int goodsid) {
		for(PLPInfo plp : getPlps().values()) {
			if(plp.getGoodsid() == goodsid) {
				if(!getPlps().containsKey(plp.getId())) {
					return DrawStatus.cannotget.getStatus();
				}			
				
				if(plp.getBuytime() != null) {
					return DrawStatus.hadget.getStatus();
				}
				
				if(plp.getLock() == null) {
					return DrawStatus.canget.getStatus();
				}
			}
		}

		return DrawStatus.cannotget.getStatus();
	}
	
	/**
	 * 锁单
	 * @param goodsid
	 */
	public void lockPayLevelPackage(int goodsid) {
		for(PLPInfo plp : getPlps().values()) {
			if(plp.getGoodsid() == goodsid) {
				plp.setLock(DateTime.now());
				set();
			}
		}
	}
	
	/**
	 * 解锁
	 * @param goodsid
	 */
	public void unlockOrder(int goodsid) {
		unlockOrderAndSetBuyTime(goodsid, null);
	}
	
	/**
	 * 解锁单 加 设置购买时间
	 * @param goodsid
	 * @param buytime
	 */
	public void unlockOrderAndSetBuyTime(int goodsid, DateTime buytime) {
		for(PLPInfo plp : getPlps().values()) {
			if(plp.getGoodsid() == goodsid) {
				plp.setLock(null);
				if(buytime != null) {
					plp.setBuytime(buytime);;		
				}
				set();
				return;
			}
		}
	}
	
	// redis ---------------------------------------------------------

	static private String key(String playerCode) {
		return String.format("PLAYER:%s:ACTIVE:%S", playerCode, acttype);
	}

	public void set() {
		try {
			setv(key(getPlayerCode()), this);
			check();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取 ActiveModule_限时抢购
	 * @param playerCode
	 * @return
	 */
	static public ActiveModule_支付等级礼包 get(String playerCode, Boolean isNewPlayer) {
		try {
			ActiveModule_支付等级礼包 module = getv(key(playerCode));
			if (module == null) {

				//新用户不需要去查数据库
				if(!isNewPlayer){
					module = MySQLCacheDAO.getV(key(playerCode), ActiveModule_支付等级礼包.class);
					if(module != null){module.set();}//同步到redis
				}

				if (module == null) {
					module = create(playerCode);
				}
			}

			if (module == null) {
				return null;
			}

			module.update();
			module.check();
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/** 查看是否有可领取奖励 控制红点 */
	public void check() {
		for(PLPInfo plp : getPlps().values()) {
			if(plp.isOpen()) {
				new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.开启);
				return;
			}
		}
		
		new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.关闭);
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				ActiveModule_支付等级礼包 activeModule_支付等级礼包 = getv(key(playerCode));
				if (activeModule_支付等级礼包 != null) MySQLCacheDAO.setV(key, activeModule_支付等级礼包);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}

}
